#ifndef _PBVP_PDBFILE_PLUGIN_H
#define _PBVP_PDBFILE_PLUGIN_H

#include <QFile>
#include <QByteArray>

#include "../../data/fileplugin.h"
#include "../../data/proteindata.h"

namespace PBVP {
	///
	class PDBFilePlugin : public QObject, public FilePluginInterface 
	{
		Q_OBJECT
		Q_INTERFACES(PBVP::FilePluginInterface)
	public:
		PDBFilePlugin();
		///
		~PDBFilePlugin();

		virtual QString description() const {
			return QString("Read PDB file");
		}

		/// 
		virtual void initialize(MainInterface* pMainInterface);

		/// 
		virtual void unload() {}

		/// 
		virtual DataSetType type() const {
			return PROTEIN_DATA;
		}

		virtual QString extension() const {
			return QString(".pdb");
		}

		/// 
		virtual DataInterface* loadFile(const QString &fname);

		/// 
		virtual bool saveFile(DataInterface* pData, const QString &fname);

	private:
		typedef enum { 
			PDB_HEADER = 0, 
			PDB_REMARK, 
			PDB_ATOM, 
			PDB_HETATM,
			PDB_CONECT, 
			PDB_UNKNOWN, 
			PDB_END, 
			PDB_EOF, 
			PDB_CRYST1,
			PDB_SHEET,
			PDB_TURN,
			PDB_SEQRES,
			PDB_HET,
			PDB_HELIX,
			PDB_COMPND
		} PDBLineType;
		///
		PDBLineType readPDBLine(QFile& file, QByteArray& line);
		///
		void parseHeader(ProteinData *pProtein, const QByteArray& line);
		///
		void parseCompound(ProteinData *pProtein, const QByteArray& line);
		///
		void parseResidues(ProteinData *pProtein, QByteArray& line);
		///
		void parseAtoms(ProteinData *pProtein, QByteArray& line);
		///
		void parseHetAtoms(ProteinData *pProtein, QByteArray& line);
		///
		void parseHelix(ProteinData *pProtein, QByteArray& line);
		///
		void parseSheet(ProteinData *pProtein, QByteArray& line);
		/// Get section of the bytearray
		QByteArray subArray(const QByteArray& line, int start, int len);

		char curChain;
		ChainData *pChain;
		int curResId;

		QString curLigand;
		LigandData *pLigand;
	};
}

#endif